//
//  FlagCollectionViewController.m
//  CollectionFlag
//
//  Created by student on 16/3/9.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "FlagCollectionViewController.h"
#import "FlagCollectionViewCell.h"
#import "ViewController.h"
#import "SectionHeader.h"


@interface FlagCollectionViewController ()

@property (nonatomic,strong) NSArray *continentNameArrs;//洲的数组

@property (nonatomic,strong) NSDictionary *flagsArrDict;//国旗的字典


@end

@implementation FlagCollectionViewController

static NSString * const reuseIdentifier = @"Cell";

//洲数组的懒加载
- (NSArray *)continentNameArrs
{
    if(_continentNameArrs == nil)
    {
        _continentNameArrs = @[@"African Flags",@"Asian Flags",@"Australasian Flags",@"European Flags",@"North American Flags",@"South American Flags"];
    }
    return _continentNameArrs;
}


//国旗的懒加载
- (NSDictionary *)flagsArrDict
{
   if(_flagsArrDict == nil)
   {
       NSString *path = [[NSBundle mainBundle] pathForResource:@"flag" ofType:@"plist"];
       NSArray *arr = [NSArray arrayWithContentsOfFile:path];
       int i =0;
       NSMutableDictionary *mArr = [NSMutableDictionary dictionary];
       for(NSArray *tmpArr in arr)
       {
           NSMutableArray *mArr1 = [NSMutableArray array];
           for(NSDictionary *dict in tmpArr)
           {
               FlagModel *f = [FlagModel flagWithDict:dict];
               [mArr1 addObject:f];
           }
           NSString *continentName = self.continentNameArrs[i++];
           mArr[continentName] = mArr1;
       }
       _flagsArrDict = mArr;
   }
    return _flagsArrDict;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    UICollectionViewFlowLayout *fl = [[UICollectionViewFlowLayout alloc] init];
    fl.itemSize = CGSizeMake(150, 120);
    //Cell之间的间距
    fl.minimumInteritemSpacing = 10.0;
    //滑动方向
    fl.scrollDirection = UICollectionViewScrollPositionCenteredVertically;//垂直方向
    //段头大小
    fl.headerReferenceSize = CGSizeMake(100, 75);
    //段尾大小
    fl.footerReferenceSize = CGSizeMake(100, 75);
    
    self.collectionView.collectionViewLayout = fl;
    // 为CollectionView的段头创建模板
    // 如果在Storyboard中设计了段头,则不需要该行代码
//    [self.collectionView registerClass:[SectionHeader class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header"];
    
}


#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 6;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSArray *flag = self.flagsArrDict[self.continentNameArrs[section]];
    return flag.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"111111");
    FlagCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    NSString *continentName = self.continentNameArrs[indexPath.section];
    NSArray *arr = self.flagsArrDict[continentName];
    FlagModel *f = arr[indexPath.row];
    cell.flag = f;
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"22222");
    if([kind isEqualToString:UICollectionElementKindSectionHeader])
    {
        SectionHeader *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header" forIndexPath:indexPath];
        header.label.text = self.continentNameArrs[indexPath.section];
        return header;
    }
    else if([kind isEqualToString:UICollectionElementKindSectionFooter])
    {
        UICollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Footer" forIndexPath:indexPath];
        return footer;
    }
    return nil;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"333333");
    // 得到当前点选国家的信息
    NSString *continentName = self.continentNameArrs[indexPath.section];
    NSArray *flagsArr = self.flagsArrDict[continentName];
    FlagModel *flag = flagsArr[indexPath.row];
    
    // 把flag信息传递到前一个页面
    
    // 3. 调用代理人执行代理方法，将数据传递回去
    [self.delegate changeFlag:flag];
    
    // 把当前页面给dismis掉
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
